package com.android.aishangauction.fragment.group;


import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.aishangauction.model.group.SortLotteryResultData;
import com.android.aishangauction.model.group.SortLotteryResultDataCallback;
import com.android.aishangauction.utils.HttpUtils;
import com.android.aishangauction.utils.MyLog;
import com.android.aishangauction.utils.ObjectUtils;
import com.android.aishangauction.fragment.BaseLazyFragment;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.R;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 中奖名单
 */
public class Fragment_Main_Type_Result extends BaseLazyFragment {


    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.money_after)
    TextView moneyAfter;
    @BindView(R.id.money_before)
    TextView moneyBefore;
    @BindView(R.id.list_free_result)
    NestFullListView listFreeResult;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;

    @BindView(R.id.result_list_loadmore_pb)
    ProgressBar resultListLoadmorePb;
    @BindView(R.id.result_list_loadmore_tv)
    TextView resultListLoadmoreTv;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;

    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;

    private static final String ARG_PARAM1 = "param1";
    private String resultid;
    private boolean networkConnected;
    private int page = 1;


    public Fragment_Main_Type_Result() {


    }

    public static Fragment_Main_Type_Result newInstance(String param1) {
        Fragment_Main_Type_Result fragment = new Fragment_Main_Type_Result();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            resultid = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {


            GetListData(page, resultid);

    }


    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_main_type_result, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        moneyBefore.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        return view;
    }


    private void initUI() {

        listFreeResult.setAdapter(new NestFullListViewAdapter<SortLotteryResultData.DataBean.ListBean>(R.layout.item_list_sort_free_result, listdata) {
            @Override
            public void onBind(int position, SortLotteryResultData.DataBean.ListBean testBean, NestFullViewHolder holder) {

                holder.setText(R.id.item_list_sort_free_result_phone, testBean.getMobile());
                holder.setText(R.id.item_list_sort_free_result_number, testBean.getOrder_sn());
                holder.setText(R.id.item_list_sort_free_result_name, testBean.getUsername());
                ObjectUtils.photoCircle(mcontext, testBean.getAvatar(), ((ImageView) holder.getView(R.id.item_list_sort_free_result_img)));


            }
        });

        LoadMore();

    }

    private void LoadMore() {


    }

    private boolean isLoading = false;
    private ArrayList<SortLotteryResultData.DataBean.ListBean> listdata = new ArrayList<>();


    private void GetListData(final int page, String id) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.MAINLIST_GOODLUCK + "?page=" + page + "&id=" + id;
        MyLog.e("地址", address);
        if (networkConnected) {

            HttpUtils.get(address, null, new SortLotteryResultDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SortLotteryResultData response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0 && response.getData() != null) {

                        String img_cover = response.getData().getGoods().getImg_cover();
                        title.setText(response.getData().getGoods().getName());
                        String price1 = response.getData().getGoods().getTeam_price();
                        String price2 = response.getData().getGoods().getPrice();
                        moneyAfter.setText("￥" + price1);
                        moneyBefore.setText("￥" + price2);
                        ObjectUtils.photo(mcontext, img_cover, img);
                        if (response.getData().getList().size() == 0 && page == 1) {
                            isLoading = false;
                            return;
                        }
                        if (page == 1) {
                            listdata.clear();
                        }

                        if (response.getData().getList().size() > 0) {
                            isLoading = true;
                            listdata.addAll(response.getData().getList());
                            listFreeResult.updateUI();
                        } else {
                            isLoading = false;
                            resultListLoadmoreTv.setVisibility(View.VISIBLE);
                            resultListLoadmoreTv.setText("已经到底了");
                            resultListLoadmorePb.setVisibility(View.GONE);
                        }

                    } else {

                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }

                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "网络连接出现异常");

        }
    }


    @OnClick({R.id.result_list_loadmore_tv, R.id.click_resetnetwork_refresh})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.result_list_loadmore_tv:
                if (isLoading) {
                    resultListLoadmoreTv.setVisibility(View.GONE);
                    resultListLoadmorePb.setVisibility(View.VISIBLE);
                    page = page + 1;
                    GetListData(page, resultid);
                } else {
                    resultListLoadmoreTv.setText("已经到底了");
                }

                break;
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                page = 1;
                GetListData(page, resultid);
                break;
        }
    }


}
